package com.qfedu.mesadmin.md.item.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qfedu.mes.common.PageVO;
import com.qfedu.mes.entity.md.MdItem;
import com.qfedu.mesadmin.md.item.dao.MdItemDAO;
import com.qfedu.mesadmin.md.item.params.AddItemParam;
import com.qfedu.mesadmin.md.item.params.ItemPageParam;
import com.qfedu.mesadmin.md.item.service.ItemService;
import com.qfedu.mesadmin.md.item.vo.ItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private MdItemDAO mdItemDAO;

    @Override
    public PageVO<ItemVO> pageItem(ItemPageParam param) {
        // 分页
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<ItemVO> itemVOS = mdItemDAO.selectItemVO();

        Page pageInfo = (Page) itemVOS;

        return new PageVO<>(pageInfo.getTotal(), itemVOS);
    }

    @Override
    public String createItemCode() {
        // 获取年月的字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String yearAndMonth = sdf.format(new Date());
        // 当前时间戳的后四位
        String t = String.valueOf(System.currentTimeMillis());
        t = t.substring(t.length() - 4);

        return "IF" + yearAndMonth + t;
    }

    @Override
    public void addItem(AddItemParam addItemParam) {

        MdItem mdItem = new MdItem();
        BeanUtils.copyProperties(addItemParam, mdItem);

        mdItemDAO.insert(mdItem);
    }

    @Override
    public void deleteItemByTypeId(Long typeId) {
        // int a = 10 / 0;
        mdItemDAO.deleteByTypeId(typeId);

    }
}
